<template>
  <div class="container">
    <el-card class="box-card">
      <el-form class="all" label-width="80px">
        <div class="left">
          <el-form-item class="label" label="关键字">
            <el-input v-model="keyword"></el-input>
          </el-form-item>
        </div>
        <div class="right">
          <el-form-item>
            <el-button @click="clear()" size="small">清除</el-button>
            <el-button @click="search()" size="small" type="primary">搜索</el-button>
          </el-form-item>
        </div>
        <el-alert :title="`数据一共${total}条`" :closable="false" type="info" show-icon></el-alert>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column width="220px" prop="id" label="编号"></el-table-column>
          <el-table-column :formatter="questionFormType" prop="questionType" label="题型"></el-table-column>
          <el-table-column width="220px" label="题目编号">
            <template slot-scope="scope">
              <div v-for="item in scope.row.questionIDs" :key="item.number">
                <a
                  href="#"
                  @click.prevent="openPreviewDialog(item)"
                  style="color:#09f;"
                >{{item.number}}</a>
              </div>
            </template>
          </el-table-column>
          <el-table-column width="200px" prop="addTime" label="录入时间"></el-table-column>
          <el-table-column prop="totalSeconds" label="答题时间(s)"></el-table-column>
          <el-table-column prop="accuracyRate" label="正确率(%)"></el-table-column>
          <el-table-column prop="userName" label="录入人"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                plain
                type="danger"
                size="small"
                circle
                icon="el-icon-delete"
                @click="delQuestion(scope.row)"
                title="删除"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div class="page-right">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="page"
          :page-size="pagesize"
          :page-sizes="[20, 30, 50, 100]"
          background
          layout="prev, pager, next, sizes, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
    <questionsPreview ref="questionPreview" :data="questionInfo"></questionsPreview>
  </div>
</template>

<script>
import { randoms, removeRandoms } from '@/api/hmmm/questions'
import questionsPreview from '../components/questions-preview.vue'
export default {
  components: {
    questionsPreview
  },
  data () {
    return {
      questionInfo: {},
      total: 0,
      currentPage: 4,
      tableData: [],
      page: 1,
      pagesize: 10,
      keyword: ''
    }
  },
  async created () {
    this.randoms()
  },
  methods: {
    async delQuestion (tableData) {
      await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await removeRandoms({ id: tableData.id })
      this.$message.success('删除成功!')
      this.randoms()
    },
    openPreviewDialog (questionInfo) {
      this.questionInfo = questionInfo
      this.$nextTick(() => {
        this.$refs.questionPreview.open()
      })
    },
    async randoms () {
      const { data } = await randoms({
        page: this.page,
        pagesize: this.pagesize,
        keyword: this.keyword
      })
      console.log(data)
      this.tableData = data.items
      this.total = data.counts
    },
    clear () {
      this.keyword = null
    },
    search () {
      this.page = 1
      this.randoms()
    },
    questionFormType (row, column, cellValue, index) {
      return cellValue === '1' ? '单选' : cellValue === '2' ? '多选' : '简答'
    },
    handleSizeChange (val) {
      this.page = 1
      this.pagesize = val
      this.randoms()
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange (val) {
      this.page = val
      this.randoms()
      console.log(`当前页: ${val}`)
    }
  }
}
</script>

<style scoped lang='less'>
.box-card {
  margin: 10px 10px;
}
.all {
  display: inline;
}
.right {
  display: flex;
  justify-content: right;
}
.label {
  text-align: right;
  vertical-align: middle;
  float: left;
  font-size: 14px;
  color: #606266;
  padding: 0 12px 0 0;
  box-sizing: border-box;
}
.page-right {
  padding: 10px 0px 0px 0px;
  display: flex;
  justify-content: flex-end;
}
</style>
